<?php
namespace pay\ali;

use Alipay\EasySDK\Kernel\Factory;
use Alipay\EasySDK\Test\TestAccount;
use Alipay\EasySDK\Kernel\Config;
use think\Db;

class AliPay
{
    public $return_url = '';
    public $notify_url = '';
    public function __construct()
    {
        $pay_way = Db::name('pay_way')->where(['type'=>'ali'])->find();
        $config_set = json_decode($pay_way['config'],true);

        $options = new Config();
        $options->notifyUrl = getCurUrl().'/home/Notify/notify_ali';
        $options->protocol = 'https';
        $options->gatewayHost = 'openapi.alipay.com';
        $options->appId = $config_set['appid'];//'<-- 请填写您的AppId，例如：2019022663440152 -->';
        $options->signType = 'RSA2';
        //公钥
        $public_key = file_get_contents(ROOT_PATH.$config_set['public_key']);
        //私钥
        $private_key = file_get_contents( ROOT_PATH.$config_set['private_key']);

        $options->alipayPublicKey = $public_key;//'<-- 请填写您的支付宝公钥，例如：MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAumX1EaLM4ddn1Pia4SxTRb62aVYxU8I2mHMqrcpQU6F01mIO/DjY7R4xUWcLi0I2oH/BK/WhckEDCFsGrT7mO+JX8K4sfaWZx1aDGs0m25wOCNjp+DCVBXotXSCurqgGI/9UrY+QydYDnsl4jB65M3p8VilF93MfS01omEDjUW+1MM4o3FP0khmcKsoHnYGs21btEeh0LK1gnnTDlou6Jwv3Ew36CbCNY2cYkuyPAW0j47XqzhWJ7awAx60fwgNBq6ZOEPJnODqH20TAdTLNxPSl4qGxamjBO+RuInBy+Bc2hFHq3pNv6hTAfktggRKkKzDlDEUwgSLE7d2eL7P6rwIDAQAB -->';

        $options->merchantPrivateKey = $private_key;

        Factory::setOptions($options);
    }
    public function wapay($params){
        $goods_name = $params['payname']??'';
        $pay_money = $params['pay_price']??'';
        $order_sn = $params['order_no']??'';
        try {
            $create = Factory::payment()->common()->create($goods_name,
                microtime(), $pay_money, $order_sn);
            $result = Factory::payment()->wap()->pay($goods_name, $create->outTradeNo, $pay_money, $this->notify_url, $this->return_url);
            return ['status'=>1,'data'=>$result->body];
        }catch (\Exception $e) {
            return ['status'=>0,'msg'=>$e->getMessage()];
        }
    }
    public function apppay($params){
        try {
            $goods_name = $params['payname']??'';
            $pay_money = $params['pay_price']??'';
            $order_sn = $params['order_no']??'';
            writeLog('请求数据'.''.$goods_name. $order_sn. $pay_money,'ali_pay');
            $result = Factory::payment()->app()->pay($goods_name, $order_sn, $pay_money);
            writeLog('请求成功','ali_pay');
            writeLog(json_encode($result),'ali_pay');
            writeLog($result->body,'ali_pay');
            return ['status'=>1,'data'=>$result->body];
        }catch (\Exception $e) {
            writeLog('请求失败','ali_pay');
            writeLog(json_encode($e),'ali_pay');
            writeLog($e->getMessage(),'ali_pay');
            return ['status'=>0,'msg'=>$e->getMessage()];
        }
    }
    public function ali_pay($params){
        $goods_name = $params['payname']??'';
        $pay_money = $params['pay_price']??'';
        $order_sn = $params['order_no']??'';
        $result = Factory::payment()->app()->pay($goods_name, $order_sn, $pay_money);
        return $result;
    }

    public function notify_ali($params){
        $result = Factory::payment()->common()->verifyNotify($params);
        return $result;
    }

}